using System;
using System.Collections.Generic;
using System.Text;

namespace PersistenceObject.Framework.Expressions
{
    public class InTableFieldExpression : Expression
    {
        public InTableFieldExpression()
        {
            this.isNotIn = false;
        }

        public InTableFieldExpression(string name, string intalbe, string infield)
        {
            this.isNotIn = false;
            this.Name = name;
            this.InTable = intalbe;
            this.InField = infield;
        }

        public override string GetFilter(IDbDriver dbOperator)
        {
            string filter = this.GetSubString(dbOperator);
            string isNotIn = "";
            if (this.IsNotIn)
            {
                isNotIn = " Not ";
            }
            string condition = string.Concat(new string[] { this.Name, isNotIn, " in (select ", this.InField, " from ", this.InTable });
            if (filter != "")
            {
                return string.Format("{0} where {1})", condition, filter);
            }
            return string.Format("{0})", condition);
        }
        
        private string inField;
        public string InField
        {
            get
            {
                return this.inField;
            }
            set
            {
                this.inField = value;
            }
        }

        private string inTable;
        public string InTable
        {
            get
            {
                return this.inTable;
            }
            set
            {
                this.inTable = value;
            }
        }

        private bool isNotIn;
        public bool IsNotIn
        {
            get
            {
                return this.isNotIn;
            }
            set
            {
                this.isNotIn = value;
            }
        }

        #region Only for SqlServer2005 ServiceBroker
        public override string ServiceBrokerGetFilter(IDbDriver dbOperator)
        {
            string filter = this.ServiceBrokerGetSubString(dbOperator);
            string isNotIn = "";
            if (this.IsNotIn)
            {
                isNotIn = " Not ";
            }
            string condition = string.Concat(new string[] { this.Name, isNotIn, " in (select ", this.InField, " from ", this.InTable });
            if (filter != "")
            {
                return string.Format("{0} where {1})", condition, filter);
            }
            return string.Format("{0})", condition);
        }
        #endregion

    }
}